package com.sen.paydemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.paydemo.entity.PaymentInfo;
import com.sen.paydemo.mapper.PaymentInfoMapper;
import com.sen.paydemo.service.PaymentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付记录Service实现
 *
 * @author 韩宗森
 * @since 2024-01-09 19:53:30
 */
@Service
@Slf4j
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    // 记录支付日志
    @Override
    public void createPaymentInfo(String plainText) {
        log.info("记录支付日志");
        Map<String, Object> plainTextMap = JSON.parseObject(plainText, HashMap.class);

        // 商户系统内部订单号
        String orderNo = (String) plainTextMap.get("out_trade_no");
        // 微信支付系统生成的订单号
        String transactionId = (String) plainTextMap.get("transaction_id");
        // 交易类型
        String tradeType = (String) plainTextMap.get("trade_type");
        // 交易状态
        String tradeState = (String) plainTextMap.get("trade_state");

        // 订单金额信息
        Map<String, Object> amount = (Map) plainTextMap.get("amount");

        // 用户实际支付金额，单位为分
        Integer payerTotal = (Integer) amount.get("payer_total");


        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setTradeType(tradeType);
        paymentInfo.setTradeState(tradeState);
        paymentInfo.setPayerTotal(payerTotal);
        paymentInfo.setContent(plainText);

        baseMapper.insert(paymentInfo);
    }
}
